<?php
class permission
{

    
    function initialize() {
        
        $ci =& get_instance();
        $class = $ci->uri->rsegments;
        $ci->load->library('session');
        $ci->load->library('fclass');
        $datauser = $ci->session->userdata('login');
		
		/*
		if(isset($_REQUEST["token"])&&$datauser!=true){
				$result = $ci->fclass->getContent("http://localhost/ht/hrm/api/login_token?token=".$_REQUEST["token"]."&app_name=AM_SALE_SUPPORT");				
				if(isset($json["result"]) && $json["result"] == true){
					$token = $json["token"];
					$session = $json["user_info"];
					//print_r($session);
					$ci->session->set_userdata('login',$json["result"]);
					$ci->session->set_userdata('user_info',$session);
					$ci->session->set_userdata('user_token',$token);
					$ci->session->set_userdata('uId',$session["user_id"]);
					//$this->session->set_userdata('parentId',$session["parentId"]);
					$room_code = $session["room_code"];
					$user_parent_id = array();
					$user_parent_id[]=$session["user_id"];
					if($session["user_type"]==1){
						$user  = file_get_contents("http://localhost/ht/hrm/api/search_user_by_app?token=".$token."&app_name=AM_SALE_SUPPORT");
						$us_array = json_decode($user,true);					
						if($us_array["result"]==true){
						foreach($us_array["users"] as $val){
								if(strlen($val["room_code"])>=strlen($room_code)){
									if(substr($val["room_code"],0,strlen($room_code))==$room_code&&$val["user_id"]!=$session["user_id"]){
										$user_parent_id[] = $val["user_id"];
									}
								}
							}
						}
					}
					//print_r($user_parent_id);
					$ci->session->set_userdata('parentId',$user_parent_id);
					//redirect(base_url().'dashboard/', 'refresh');
			}
		}
		*/
		
		
		
		
		
		
		
		
		
		
		
		
		
        $defauAccess=array("login","logout","lang","apis");

        $user =  $ci->session->userdata('user_info');
		//print_r($user["permission_apps"]);
	// echo $user["full_name"]." (".$user["room_name"].")";
	
        if(in_array($class[1] , $defauAccess))
        {
           
        }else{
		
		if($ci->session->userdata('expire_date')!=""){
			$exp = strtotime($ci->session->userdata('expire_date'))-time();
			if($exp<=0){
				$ci->session->sess_destroy();		
				redirect("http://login.alta.com.vn/?redirect=http://crm.altamedia.vn&out", 'refresh');	
			}
		}
		
		$per = $user["permission_apps"];
		$per_controler = array();
		foreach($per as $id=>$val){
			if($id=="AM_SALE_SUPPORT"){
				$per_controler = $val;
			}
		}
		
		 $per_controler=$this->convertArray($user["permission_apps"]);
		// print_r($per_controler);
		 

            // $per_controler=array();
			 /*
            if($ci->fclass->check_permission('special','is_ceo',$ci->session->userdata('permissions2'),$ci->session->userdata('permissions_base'))){
                $per_controler = array(
                                    "customer"      => array("index","detail","add","add_form","insert","update","move_customer","remove_customer","update_sale","create_acc","remove_img_cmnd","get_list_wall","list_sale","list_full_sale","full_sale_view","remove_sale","add_sale","edit_sale_fix","add_sale_fix"),
                                    "companies"     => array("index","view","detail","insert","update","remove_comp","remove_img_verify","remove_img_tax"),
                                    "dashboard"     => array("index","view","wall"),
                                    "notification"  => array("index"),
                                    "apis"          => array("index","upload_comp","upload_tax","upload_cmnd","update_wall"),
                                    "timeline"      => array("index","view","add_feeds"),
                                    "users"         => array("index","detail"),
                                    "conflict"      => array("index","view","review","set_sale_customer"),
                                    "history"      => array("index","view")
                                    );


            }else if($ci->fclass->check_permission('special','is_accounting',$ci->session->userdata('permissions2'),$ci->session->userdata('permissions_base'))){
                $per_controler = array(
                                    "customer"      => array("index","detail","add","add_form","insert","update","move_customer","remove_customer","update_sale","remove_img_cmnd","get_list_wall","list_sale","list_full_sale","full_sale_view","remove_sale","add_sale","edit_sale_fix","add_sale_fix"),
                                    "companies"     => array("index","view","detail","insert","update","remove_comp","remove_img_verify","remove_img_tax","keep_comp"),
                                    "dashboard"     => array("index","view","wall"),
                                    "notification"  => array("index"),
                                    "apis"          => array("index","upload_comp","upload_tax","upload_cmnd","update_wall"),
                                    "timeline"      => array("","",""),
                                    "users"         => array("",""),
                                    "conflict"      => array("index","view","review","set_sale_customer"),
                                    "history"      => array("index","view")
                                    );

            }else if($ci->fclass->check_permission('special','is_sale',$ci->session->userdata('permissions2'),$ci->session->userdata('permissions_base'))){
                 $per_controler = array(
                                    "customer"      => array("index","detail","add","add_form","insert","update","move_customer","remove_customer","update_sale","create_acc","remove_img_cmnd","get_list_wall","list_sale","list_full_sale","full_sale_view","remove_sale","add_sale","edit_sale_fix","add_sale_fix"),
                                    "companies"     => array("index","view","detail","insert","","","",""),
                                    "dashboard"     => array("index","view","wall"),
                                    "notification"  => array("index"),
                                    "apis"          => array("index","upload_comp","upload_tax","upload_cmnd","update_wall"),
                                    "timeline"      => array("index","view","add_feeds"),
                                    "users"         => array("index","detail"),
                                    "conflict"      => array("index","view","review","set_sale_customer"),
                                    "history"      => array("index","view")
                                    );
            }
		*/
            // $session['per_view'] = array("cust_phone","cust_email","cust_bank_account","cust_cmnd");
            // Phong dieu hanh
             //if(isset($ci->session->userdata('rId'))){
            //    if($ci->session->userdata('rId')=="1.1.1"){
                //    $session['per_view'] = array();
             //   }
            // }
            

           // $ci->session->set_userdata($session);
		if(count($per_controler)>0){
            $nameKey=array_keys($per_controler);
            $checking=false;
			
				foreach($per_controler as $key=> $value){
				   if($key==$class[1]){
						if(in_array($class[2], $value)){
							$checking=true;
						}
				   }
				}
			}
           
           if($checking==false){
            echo json_encode(array("result"=>false,"msg"=>"Bạn không có quyện thực hiện thao tác này."));
            exit();
           }
            
        }
        
    }
	
	public function convertArray($array){
		$ar = array();
		foreach($array as $key =>$val){			
			foreach($val as $k =>$v){
				foreach($v as $kk =>$vv){
					$ar[$k][] = $kk;
				}
			}
		}
		return $ar;
	}
}